<?php 
if( empty( $_GET ['dialogue'] ) )
{
	header( 'Location: init.php' );
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html" />
	<title>

	</title>
</head>
<body>
	<div>
	<?php
		$dialogue = file( dirname( __FILE__ ) . '\\script\\' . $_GET ['dialogue'] . '.advs' , FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES );
		?>
		<?php
			function generateHTMLArgs($page)
			{
				$flag = false;
				$result = "";
				foreach ( $_GET as $key => $value )
				{
					if( $result == "" )
					{
						$result = "?$key=$value";
					}
					else
					{
						$result .= "&$key=$key";
					}
				}
				return $page . $result;
			}
			$flag = true;
			$dialoguePos = -1;
				for ( $i = 0 ; $i < count( $dialogue ) ; $i = $i + 1 )
				{
					
					if( $dialogue [$i] [0] == '#' ) continue;
					if ( $dialogue [$i] [0] == '(' )
					{
						$flag = false ;
					}
					else if ( $dialogue [$i] [0] == ')' )
						{
							$flag = true;
							for(; $i < count( $dialogue ) ; $i++)
							{
								
								if( $dialogue [$i] [0] == '{' )
								{
									for( $j = $i + 1 ; $j < count( $dialogue )  ; $j ++ )
									{
										if( $dialogue [$j] [0] == '}' ) break;
										$data = explode( ' ' , str_replace( '<br>' , '<br />' , str_replace( 'UName' , $_GET ['username'] , $dialogue [$j] ) ) , 2 );
										echo '<br />';
										echo '<a href="index.php?dialogue=' . $data[1] . '&position=1&username=' . $_GET['username'] . '">' . $data[0] . '</a>';

									}
									break;
								}
							}
							break;
						}
						else
						{
							if( $flag == false )
							{
								$dialoguePos ++;
								if ( $dialoguePos == $_GET ['position'] )
								{
									if( empty( $_GET['speed'] ) )
									{
										$_GET['speed'] = 20;
									}
									$data = explode( ' ' , str_replace( '<br>' , '<br />' , str_replace( 'UName' , $_GET ['username'] , $dialogue [$i] ) ) , 2 );
									echo '<p>' . ( ( empty( $data [0] ) ) ? ' ' : $data[0] ) . '</p>' ;
									echo '<br />';
									echo '<p>';
									if( $_GET['speed'] == -1 )
									{
										echo ( empty( $data [1] ) ? ' ' : $data [1]  );
									}
									else
									{
										if( ! empty ( $data [1] ) )
										{
											for ( $j = 0 ; $j < strlen( $data [1] ) ; $j ++ )
											{
												echo $data [1] [$j];
												usleep( $_GET['speed'] * 10 );
											}
										}
									}
									echo '</p>';
									echo '<a tabindex="4" href="index.php?dialogue=' . $_GET['dialogue'] . '&position=' . ( $_GET['position'] + 1 ) . '&username=' . $_GET['username'] . '&speed=' . $_GET['speed'] . '">下一部分</a>';
									break;
								}
							}
						}
					}
		?>
	</div>
	<a href="version.php">版本信息</a>
	<?php 	echo '<a href="init.php?dialogue=' . $_GET['dialogue'] . '&position=' . $_GET['position'] . '&username=' . $_GET['username'] . '&speed=100">设置</a>';?>
</body>

</html>